package com.charger.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.domain.Dispose;
import com.charger.service.IDisposeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.Failure;
import com.charger.service.IFailureService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 故障管理Controller
 *
 * @author xuan
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/risksFailure/failureController")
public class FailureController extends BaseController {
    @Autowired
    private IFailureService failureService;
    @Autowired
    private IDisposeService disposeService;
    @Autowired
    private IChargingstationService chargingstationService;

    /**
     * 查询故障管理列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureController:list')")
    @GetMapping("/list")
    public TableDataInfo list(Failure failure) {
        startPage();
        List<Failure> list = failureService.selectFailureList(failure);
        list.forEach(fai -> {
            // 修改处理表
            if (fai.getDisposeId() != null)
                fai.setDispose(disposeService.selectDisposeById(fai.getDisposeId()));
            // 修改充电站
            if (fai.getChargerStationId() != null)
                fai.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(fai.getChargerStationId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出故障管理列表
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureController:export')")
    @Log(title = "故障管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Failure failure) {
        List<Failure> list = failureService.selectFailureList(failure);
        ExcelUtil<Failure> util = new ExcelUtil<Failure>(Failure.class);
        util.exportExcel(response, list, "故障管理数据");
    }

    /**
     * 获取故障管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureController:query')")
    @GetMapping(value = "/{failureId}")
    public AjaxResult getInfo(@PathVariable("failureId") Long failureId) {
        Failure failure = failureService.selectFailureByFailureId(failureId);
        // 修改处理表
        if (failure.getDisposeId() != null)
            failure.setDispose(disposeService.selectDisposeById(failure.getDisposeId()));
        // 修改充电站
        if (failure.getChargerStationId() != null)
            failure.setChargingstation(chargingstationService.selectChargingstationByChargingstationId(failure.getChargerStationId()));
        return success(failure);
    }

    /**
     * 新增故障管理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureController:add')")
    @Log(title = "故障管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Failure failure) {
        failure.setState("未派单");
        return toAjax(failureService.insertFailure(failure));
    }

    /**
     * 修改故障管理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureController:edit')")
    @Log(title = "故障管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Failure failure) {
        if (failure.getDispose() == null) {
            // 派发
            failure.setDisposeId(failure.getDisposeId());
            failure.setState("未处理");
            // 派发添加
            Dispose parse = disposeService.selectDisposeById(failure.getDisposeId());
            Dispose dispose = new Dispose();
            dispose.setProcessorsName(parse.getProcessorsName());
            disposeService.insertDispose(dispose);
            failure.setDisposeId(disposeService.selectDisposeById2());
        } else {
            // 修改处理表
            disposeService.updateDispose(failure.getDispose());
        }
        // 修改充电站
        Chargingstation chargingstation = failure.getChargingstation();
        Long id = chargingstation.getChargingstationId();
        failure.setChargerStationId(id);
        return toAjax(failureService.updateFailure(failure));
    }

    /**
     * 删除故障管理
     */
    @PreAuthorize("@ss.hasPermi('risksFailure:failureController:remove')")
    @Log(title = "故障管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{failureIds}")
    public AjaxResult remove(@PathVariable Long[] failureIds) {
        return toAjax(failureService.deleteFailureByFailureIds(failureIds));
    }
}
